package com.cs.spzx.manager.controller;

import com.cs.spzx.manager.service.SysMenuService;
import com.cs.spzx.manager.service.SysUserService;
import com.cs.spzx.manager.service.ValidateCodeService;
import com.cs.spzx.model.dto.system.LoginDto;
import com.cs.spzx.model.entity.system.SysUser;
import com.cs.spzx.model.vo.common.Result;
import com.cs.spzx.model.vo.common.ResultCodeEnum;
import com.cs.spzx.model.vo.system.LoginVo;
import com.cs.spzx.model.vo.system.SysMenuVo;
import com.cs.spzx.model.vo.system.ValidateCodeVo;
import com.cs.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/index")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @PostMapping("login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
//    @GetMapping(value="/getUserInfo")
//    public Result<SysUser> getUserInfo(@RequestHeader(name = "authorization") String token) {
//        System.out.println(">>>>>>>>>>>>>token>>>>>>>>>>>>>>>>>:" + token);
//        SysUser sysUser = sysUserService.getUserInfo(token) ;
//        return Result.build(sysUser , ResultCodeEnum.SUCCESS) ;
//    }
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(AuthContextUtil.get()  , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping("generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "authorization") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }
    @Autowired
    private SysMenuService sysMenuService;
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }












}
